package com.edu.hre.struts.action.recruit;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.ComboBoxInit;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreJobVacancy;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.struts.form.recruit.VacancyAddForm;

public class VacancyAddAction extends Action {

	private static final Logger logger = Logger.getLogger(VacancyAddAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private final String ACTION_NAME = "vacancyAdd";
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		VacancyAddForm beanForm = (VacancyAddForm) form;
		beanForm.setIsAddNew(true);

		if(request.getParameter(StringConstants.BTN_ADD) == null){

			initFormBean(beanForm, request);
			beanForm.setStatus(1);
			beanForm.setMessage(StringConstants.BLANK);
			return mapping.findForward(ACTION_NAME);
		}
		
		if(StringUtils.isBlank(CommonUtils.normalizeString(beanForm.getName()))){
			beanForm.setMessage("Vacancy name cannot be blank!");
			beanForm.setStatus(1);
			initFormBean(beanForm, request);
			return mapping.findForward(ACTION_NAME);
		}
		
		int no = -1;
		try {
			no = Integer.parseInt(beanForm.getNoOfPositions());
		} catch (Exception e) {
			no = -1;
		}
		
		if (no <= 0) {
			beanForm.setMessage("No. of positions must be a positive integer!");
			beanForm.setStatus(1);
			initFormBean(beanForm, request);
			return mapping.findForward(ACTION_NAME);
		}
		
		HreJobVacancy tmp = new HreJobVacancy();
		tmp.setJobTitleCode(beanForm.getJobTitle());
		
		Integer managerId = CommonUtils.getEmpNumber(beanForm.getHiringManager());
		if (managerId == null || managerId <= 0) {
			beanForm.setMessage("Hiring manager is empty or invalid!");
			beanForm.setStatus(1);
			initFormBean(beanForm, request);
			return mapping.findForward(ACTION_NAME);
		}
		tmp.setHiringManagerId(managerId);
		tmp.setName(CommonUtils.normalizeString(beanForm.getName()));
		tmp.setNoOfPositions(no);
		tmp.setStatus(beanForm.getStatus());
		tmp.setDateCreated(new Date());
		tmp.setDateModified(new Date());

		boolean status = baseService.saveOrUpdate(tmp);
		if (!status) {
			beanForm.setMessage(StringConstants.INSERT_FAILURE_MESSAGE);
			initFormBean(beanForm, request);
			return mapping.findForward(ACTION_NAME);
		}
		
		response.getWriter().write(StringConstants.POP_UP_CLOSE_SCRIPT);
		return null;
	}
	
	private void initFormBean(VacancyAddForm beanForm, HttpServletRequest request) {

		beanForm.setAllJobTitles(ComboBoxInit.getAllJobTitles(false));
		beanForm.setAllManagers(ComboBoxInit.getAllEmployees(false));
		beanForm.setAllStatuses(ComboBoxInit.getAllActiveStatuses(false));
	}
}
